﻿using System;
using System.Collections.Generic;
using System.Web.UI.WebControls;
using EntitiesTM;
using Business;

namespace TimeWork.time.admin
{
    public partial class EditProjectTree : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Session["email"] == null)
            {
                Response.Redirect("http://"+Request.Url.Authority+"/Login.aspx");
            }
            if (!IsPostBack)
            {
                btnCreate.Visible = false;
                btnUpdate.Visible = false;
                btnDelete.Visible = false;
                FillData();
            }
        }

        private string getParam()
        {
            if (Request.QueryString["pid"] != null)
            {
                return Request.QueryString["pid"].ToString();
            }
            else
            {
                return "0";
            }
        }

        private void BindDdlProjectParent(string value,int exceptID)
        {
            Dictionary<string, string> data = BuProject.selProjectByParentIDTree(exceptID, Session["companyid"].ToString());
            ListItem item;
            item = new ListItem("- Gốc danh mục -","0");
            ddlParentID.Items.Add(item);

            foreach (KeyValuePair<string, string> pair in data)
            {
                item = new ListItem(pair.Key,pair.Value);
                ddlParentID.Items.Add(item);
            }
            ddlParentID.SelectedValue = value;
            
        }

        private void FillData()
        {
            // Lay projectID qua tham so pid
            int projectID = Int32.Parse(getParam());
            // Truong hop sua project
            if (projectID != 0)
            {
                lblTitle.Text = "Cập nhật";
                btnUpdate.Visible = true;
                btnDelete.Visible = true;
                try
                {
                    EnProject project = BuProject.selProjectByIDTree(projectID, Session["companyid"].ToString());
                    txtProjectName.Text = project.projectName;
                    txtProjectCode.Text = project.projectCode;
                    BindDdlProjectParent(project.projectParentID.ToString(), projectID);
                    txtProjectDescription.Text = project.projectDescription;
                }
                catch
                {
                    Response.Redirect("~/time/admin/ProjectTree.aspx");
                }
            }
            else
            {
                lblTitle.Text = "Thêm mới";
                btnCreate.Visible = true;
                BindDdlProjectParent("0",0);
            }
        }

        protected void btnCancel_Click(object sender, EventArgs e)
        {
            Response.Redirect("~/time/admin/ProjectTree.aspx");
        }

        protected void btnUpdate_Click(object sender, EventArgs e)
        {
            int projectID = Int32.Parse(getParam());
            string nameProject = txtProjectName.Text.Trim();
            
            // Chay cac thu thu tuc
            EnProject project = new EnProject();
            project.projectName = nameProject;
            project.projectDescription = txtProjectDescription.Text;
            project.projectID = projectID;
            project.projectParentID = Int32.Parse(ddlParentID.SelectedValue);
            project.projectCode = txtProjectCode.Text;

            BuProject.updProjectTree(project, Session["companyid"].ToString());
            BuProject.updProjectTreeChild(projectID, Session["companyid"].ToString());
            Response.Redirect("~/time/admin/ProjectTree.aspx");
            
        }

        protected void btnCreate_Click(object sender, EventArgs e)
        {
            // Lay cac tham so
            string nameProject = txtProjectName.Text;
            string projectDescription = txtProjectDescription.Text;
            // Chay cac thu thu tuc
            EnProject project = new EnProject();
            project.projectName = nameProject;
            project.projectDescription = projectDescription;
            project.projectClientID = 0;
            project.projectStartDate = DateTime.Now;
            project.projectEndDate = DateTime.Now;
            project.projectEnable = false;
            project.projectEnableMember = false;
            project.projectManager = 2;
            project.projectCode = txtProjectCode.Text;
            project.projectParentID = Int32.Parse(ddlParentID.SelectedValue);

            BuProject.insProject(project, Session["companyid"].ToString());
            Response.Redirect("~/time/admin/ProjectTree.aspx");
        }

        protected void btnDelete_Click(object sender, EventArgs e)
        {
            int projectID = Int32.Parse(getParam());
            EnProject project = new EnProject();
            project.projectID = projectID;
            BuProject.delProject(project, Session["companyid"].ToString());
            BuProject.delProjectChild(project, Session["companyid"].ToString());
            Response.Redirect("~/time/admin/ProjectTree.aspx");
        }
    }
}